<?php
/* 
	[BuyPg] (C) 2010-2011 Buypg
	$Id: UserAction.class.php 2011-4-20 15:53:01 wufujun $
 */

if (!defined('THINK_PATH')){
	exit('Access Denied');
}

class UserAction extends BpAction{
	public function login(){
		global $_SGLOBAL;
		$refer = empty($_GET['refer'])?rawurldecode(cookie('_refer')):$_GET['refer'];
		$ms = explode('/', $refer);
		if( in_array('Usercp',$ms) || (in_array('Bizcp' , $ms) && !in_array('renling' , $ms))){
			$refer = __APP__."/Index/";
		}
		if(empty($refer)){
			$refer = $_SGLOBAL['refer'];
			$ms = explode('/', $refer);
			if(in_array('reg', $ms) || in_array('doreg', $ms)){
				$refer = __APP__."/Index/";
			}
		}
		if($_SGLOBAL['G_uid']){
			$this->assign('jumpUrl' , $refer);
			$this->error('您已成功登录');
		}
		$this->assign('refer' , $refer);
		$this->display('buypg@User:login');
	}
	
	public function reg(){
		global $_SGLOBAL;
		if($_SGLOBAL['G_uid']){
			$this->error('您已登录，请退出后再进行此操作');
		}
		$this->display('buypg@User:reg');
	}
	
	//注册操作
	public function doreg(){
		global $_SGLOBAL;
		$DbUser = D("User");
		if($DbUser->create()){
			 if(false !== $DbUser->add()) {
                $this->assign('jumpUrl',__URL__.'/login');
				$this->success("您已经注册成功，请登录");
            }else{
                $this->error('注册失败');
            } 
		}else{
			$this->assign('jumpUrl',__URL__.'/reg');
			$this->error($DbUser->getError());
		}
	}

	//登录操作
	public function checklogin(){
		global $_SGLOBAL;
		//post 判断
		$post = $_POST;
		$username = trim($post['username']);
		$error = array();
		if(empty($username)){
			$error[] = array(
				'return_name' => 'username',
				'return_info' => '用户名不能为空'
			);
		}
		if(empty($post['password'])){
			$error[] = array(
				'return_name' => 'password',
				'return_info' => '密码不能为空'
			);
		}
		if(empty($post['verify'])){
			$error[] = array(
				'return_name' => 'verify',
				'return_info' => '验证码不能为空'
			);
		}

		//验证码判断
		if($_SESSION['verify'] != md5($_POST['verify'])) {
			$error[] = array(
				'return_name' => 'verify',
				'return_info' => '验证码错误'
			);
		}

		if(!empty ($error)){
			$this->ajaxReturn($error,'错误',0);
		}elseif(!is_array(($passport = getpassport($post['username'], $post['password'],$post['biz'])))){
			if($passport == -1){
				$error[] = array(
					'return_name' => 'username',
					'return_info' => '登录失败，用户名错误'
				);
			}elseif($passport == -2){
				$error[] = array(
					'return_name' => 'password',
					'return_info' => '登录失败，密码错误'
				);
			}
			$this->ajaxReturn($error,'错误',0);
		}else{
			$setarr['uid'] = $passport['user_id'];
			$setarr['username'] = $passport['user_nickname'];
			$setarr['password'] = $passport['user_password'];
			//设置Cookie
			cookie('auth', authcode("$setarr[password]\t$setarr[uid]", 'ENCODE'));
			cookie('loginuser',$setarr['username']);
			cookie('biz' , $post['biz']);
			cookie('siteid' , -1);
			cookie('siteflag' , -1);
			cookie('_refer' , null);
			$successmsg = array();
			$successmsg['refer'] = $post['refer'];
			$this->ajaxReturn($successmsg,'成功',1);
		}
	}

	//退出
	public function logout(){
		global $_SGLOBAL;
		clearcookie();
		cookie('_refer',null);
		$this->assign('jumpUrl' , __APP__."/Index/");
		$this->success('退出成功！');
	}

}

?>
